/* 在父组件上配上  quillvalue: {action: "/v1/file/normalUpload",
        limit: 10,
        content: ''
      },上传地址，限制个数以及富文本显示的内容就好了
 */
<template>
  <div class="quill-div">
    <c-upload class="avatar-uploader"
              :show-upload-list="false"
              :show-file-list="false"
              :on-success="handleSuccess"
              :on-error="uploadError"
              accept="image/png, image/jpeg"
              :format="['jpg','jpeg','png','gif']"
              :headers="header"
              multiple
              :action="quillvalue.action">
      <c-button icon="ios-cloud-upload-outline"></c-button>
    </c-upload>
    <c-dialog :visible.sync="dialogVisible">
      <img width="100%"
           :src="dialogImageUrl"
           alt />
    </c-dialog>
    <quill-editor v-model="content"
                  :options="editorOption"
                  ref="addTinymce"
                  @change="getFile"
                  class="ql-editor" id="addTinymce"></quill-editor>
  </div>
</template>
<script>
// 工具栏配置
import { quillEditor } from 'vue-quill-editor'
// // import editTiny from './components/Tinymce'
// // import ImageResize from 'quill-image-resize-module'
// // Quill.register('modules/imageResize', ImageResize)
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'],
  ['blockquote', 'code-block'],
  [{ header: 1 }, { header: 2 }],
  [{ list: 'ordered' }, { list: 'bullet' }],
  [{ script: 'sub' }, { script: 'super' }],
  [{ indent: '-1' }, { indent: '+1' }],
  [{ size: ['small', false, 'large', 'huge'] }], // custom dropdown
  [{ header: [1, 2, 3, 4, 5, 6, false] }],
  [{ color: [] }, { background: [] }], // dropdown with defaults from theme
  [{ font: [] }],
  [{ align: [] }],
  ['link', 'image', 'video'],
  ['clean'] // remove formatting button
]

export default {
  data () {
    return {
      Serverurl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      content: '',
      header: {
        accessToken: this.$store.state.user.token
        // ContentType:'multipart/form-data'
      },

      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions, // 工具栏
            handlers: {
              image: function (value) {
                if (value) {
                  // 调用iview图片上传
                  document.querySelector('.avatar-uploader input').click()
                } else {
                  this.quill.format('image', false)
                }
              }
            }
          }
        },
        placeholder: '请输入'
      }
    }
  },
  props: {
    quillvalue: {
      type: Object,
      default: {
        action: '',
        limit: 100,
        content: ''
      }
    }
  },
  components: {
    quillEditor
  },

  watch: {
    quillvalue: {
      handler: function (val) {
        this.content = val.content
      },
      immediate: false,
      deep: true
    }
  },
  methods: {
    handleSuccess (res) {
      // 获取富文本组件实例
      const quill = this.$refs.addTinymce.quill
      // 如果上传成功
      if (res) {
        // 获取光标所在位置
        console.log(res)
        const length = quill.getSelection().index
        // 插入图片，res为服务器返回的图片链接地址
        quill.insertEmbed(length, 'image', res.data.fileUrl)
        // 调整光标到最后
        quill.setSelection(length + 1)
      } else {
        // 提示信息，需引入Message
        console.log('图片插入失败')
        this.$message.error('图片插入失败')
      }
    },
    getFile () {
      this.$emit('getcontent', this.content)
    },
    uploadError () {
      // loading动画消失
      this.quillUpdateImg = false
      this.$message.error('图片插入失败')
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }
  }
}
</script>

<style lang='scss' >
.quill-div {
  .avatar-uploader {
    display: none;
  }
  .quill-div {
    width: 80%;
  }
  .ivu-upload {
    display: none;
  }
  .ql-container.ql-snow {
    min-height: 200px !important;
    max-height: 449px !important;
    overflow: scroll;
  }
  .ql-snow .ql-picker.ql-header .ql-picker-label::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item::before {
    content: "文本";
  }
  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
    content: "标题1";
  }
  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
    content: "标题2";
  }
  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
    content: "标题3";
  }
  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
    content: "标题4";
  }
  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
    content: "标题5";
  }
  .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
  .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
    content: "标题6";
  }

  .ql-snow .ql-picker.ql-font .ql-picker-label::before,
  .ql-snow .ql-picker.ql-font .ql-picker-item::before {
    content: "标准字体";
  }
  .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
  .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
    content: "衬线字体";
  }
  .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
  .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
    content: "等宽字体";
  }
  .ql-snow .ql-picker.ql-size .ql-picker-label::before,
  .ql-snow .ql-picker.ql-size .ql-picker-item::before {
    content: "14px";
  }
  .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
  .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
    content: "10px";
  }
  .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
  .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
    content: "18px";
  }
  .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
  .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
    content: "32px";
  }
  .ql-snow .ql-editor img {
    max-width: 100% !important;
    height: 200px !important;
  }
}
</style>
<style lang="scss" >
@import "@/assets/sass/common.scss";
.quill-div {
  .el-upload--picture-card {
    // background-color: #fbfdff;
    border: 1px dashed #c0ccda;
    border-radius: 6px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 280px !important;
    height: 148px;
    cursor: pointer;
    line-height: 146px;
    vertical-align: top;
  }
  .el-upload-list--picture-card .el-upload-list__item {
    overflow: hidden;
    background-color: #fff;
    border: 1px solid #c0ccda;
    border-radius: 6px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 280px !important;
    height: 148px;
    margin: 0 8px 8px 0;
    display: inline-block;
  }
  .content {
    @include pageInner;
    min-height: 580px;
    .header-title {
      padding-bottom: 16px;
      font-size: 16px;
      color: rgba(0, 0, 0, 0.85);
      line-height: 1;
      font-weight: bold;
    }
  }

  .back-bton {
    display: flex;
    justify-content: center;
  }
}
</style>
